<?php

namespace app\api\model;

use app\admin\model\Admin;
use app\http\GetUid;
use think\Db;
use think\db\Query;
use think\Model;

class SetAccountFlow extends Model
{
    use PaginatePassPage;

    protected $autoWriteTimestamp = true;
    protected $type = ['images' => 'json', 'business_time' => 'timestamp'];
    protected $table = 'jxc_set_account_flow';

    // 定义全局的查询范围
    protected function base($query)
    {
        $c = Admin::find(\app\http\GetUid::getUId());
        // 门店负责人可查看自己门店的所有信息
        if ($c->level == 2) {
            $maker = Admin::where('store', $c->store)->where('level', 3)->column('id');
        } elseif ($c->level == 1) {
            $maker = Admin::column('id');
        } else {
            $maker = $c->id;
        }
        $query->whereIn('maker', $maker);
    }

    public function supplierReconciliation()
    {
        return $this->belongsTo(SupplierReconciliation::class, 'business_number', 'number');
    }

    public function customerReconciliation()
    {
        return $this->belongsTo(CustomerReconciliation::class, 'business_number', 'number');
    }

    public function sale()
    {
        return $this->belongsTo(Sale::class, 'business_number', 'number');
    }

    public function saleReturn()
    {
        return $this->belongsTo(SaleReturn::class, 'business_number', 'number');
    }

    public function inStock()
    {
        return $this->belongsTo(InStock::class, 'business_number', 'number');
    }

    public function outStock()
    {
        return $this->belongsTo(OutStock::class, 'business_number', 'number');
    }

    public function purchaseReturn()
    {
        return $this->belongsTo(PurchaseReturn::class, 'business_number', 'number');
    }

    public function purchase()
    {
        return $this->belongsTo(Purchase::class, 'business_number', 'number');
    }

    public function setAccount()
    {
        return $this->belongsTo(SetAccount::class, 'set_account_id');
    }

    public function customer()
    {
        return $this->belongsTo(UserModel::class, 'customer_id')->where('customer_type', 'user');
    }

    public function supplier()
    {
        return $this->belongsTo(SupplierModel::class, 'customer_id')
            ->where('customer_type', 'supplier');
    }

    public function accountType()
    {
        return $this->belongsTo(AccountType::class, 'account_type_id');
    }

    public function getCustomerInfo($id)
    {
        $list = $this->getOne('sale', '*', ['id' => $id]);
        return $list;
    }
}
